const { SuccessModel } = require("../model/resModel");
const {
  getList,
  getDetail,
  deleteBlog,
  updateBlog,
  newBlog,
} = require("../controller/blog");

const handelBlogRouter = (req, res) => {
  const method = req.method;
  const path = req.path;
  // 获取博客列表
  if (method === "GET" && path === "/api/blog/list") {
    const author = req.query.author;
    const keyword = req.query.keyword;
    return new SuccessModel(getList(author, keyword));
  }
  // 获取某一篇博客详情
  if (method === "GET" && path === "/api/blog/detail") {
    return new SuccessModel(getDetail(req.query.id));
  }
  // 新增一篇博客
  if (method === "POST" && path === "/api/blog/new") {
    return new SuccessModel(newBlog(req.query.id));
  }
  // 更新某一篇博客
  if (method === "POST" && path === "/api/blog/update") {
    const succ = updateBlog(req.query.id);
    if (succ) {
      return new SuccessModel("更新成功");
    } else {
      return new SuccessModel("更新失败");
    }
  }
  // 删除某一篇博客
  if (method === "GET" && path === "/api/blog/del") {
    const succ = deleteBlog(req.query.id);
    if (succ) {
      return new SuccessModel("删除成功");
    } else {
      return new SuccessModel("删除失败");
    }
  }
};
module.exports = handelBlogRouter;
